<?php

$AddToFamilyId = @$_GET['AddToFamilyId'];
$IndividualId = @$_GET['IndividualId']; // not valid
$submitData = @$_POST['submitData'];

echo '<div id="page_header">'."\n";
echo '<ul>'."\n";
echo '<li class="titlename2">Add new Individual</li>'."\n";
echo '</ul>'."\n";
echo '</div>'."\n";



if($PACTS_cfg->getUserCanAdd()){

    if($submitData == "Add New Individual")
    {
        $PACTS_cfg->PMA_gpc_extract($HTTP_POST_VARS, $GLOBALS);
        //	$IndividualId = $fields['IndividualId'];

        $fields_changes = array();
        $fields_changes_prev = array();
        $fields_invalid = array();
        $fields_changes_SQL = array();

        // check fields for validity
        $bInvalidField = FALSE;
    }

    //$fields['FamilyShortName'] = trim(@$fields['FamilyShortName']);
    // check mandatory fields

    if((!@$fields['FamilyId']>0) and $submitData == "Add New Individual") {
        echo "<b>Error!: Family must be selected</b><p>";
        $fields['FamilyId']="";
    }

    if(strlen(@$fields['FirstName'])>0 and $submitData == "Add New Individual") {
        if(!ctype_alpha($fields['FirstName'][0]) ) {
            echo "<b>Error!: Invalid FirstName</b><p>";
            $fields['FirstName']="";
        }
    }

    if(strlen(@$fields['Surname'])>0 and $submitData == "Add New Individual") {
        if(!ctype_alpha($fields['Surname'][0]) ) {
            echo "<b>Error!: Invalid Surname</b><p>";
            $fields['Surname']="";
        }
    }


    if( @$fields['FamilyId']>0 and strlen(trim(@$fields['FirstName']))>0 and strlen(trim(@$fields['Surname']))>0)
    {
        if($submitData == "Add New Individual") {

            //           echo "OKK!!<pre>".$PACTS_cfg->print_ar($fields)."OKK!!</pre><p>";

            $insertSql = "INSERT INTO Individual (IndividualId, FamilyId, FirstName, Surname, GenderCode, RoleTypeCode, CreationTMS, CreationUserId, ModificationTMS, ModificationUserId) VALUES ('', '".$fields['FamilyId']."' ,'".trim($fields['FirstName'])."' ,'".trim($fields['Surname'])."' , '".$fields['GenderCode']."', '".$fields['RoleTypeCode']."',
           now(), '".$_SESSION['username']."', now(),'".$_SESSION['username']."')";

            //         echo "<p>$insertSql <p>";

            $result_insert = mysql_query($insertSql,$PACTS_cfg->dblink);
            $IndividualId = mysql_insert_id();
            $FamilyId = $fields['FamilyId'];

            if(!mysql_errno()) {
                $PACTS_cfg->InsertAuditTrailEntry("", "" ,"INSERT","Individual","COMPLETE ENTRY",$_SESSION['username'], $_SERVER['PHP_SELF'],$IndividualId);
                foreach ($fields as $fields_key => $fields_value){
                    if (strlen(trim($fields_value)) > 0 ){
                        $PACTS_cfg->InsertAuditTrailEntry("", $fields_value ,"Add","Individual",$fields_key,$_SESSION['username'], $_SERVER['PHP_SELF'],$IndividualId);
                    }
                }

                $URLmessage="Individual added sucessfully!";

                $URL = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'])."/index.php?view=Fmly_view&FamilyId=".$FamilyId."&PHPSESSID=".$PHPSESSID."&URLmessage=".$URLmessage."";

                echo '<p>Click <a href="'.$URL.'">here</a> to view individual</p>';
                echo '<script type="text/javascript">'."\n";
                echo '<!--'."\n";
                echo 'window.location = "'.$URL.'"'."\n";


                echo '//-->'."\n";
                echo '</script>'."\n";
                exit;

                //header($URL);
                exit;


                echo "<p>Individual added sucessfully!</p>";
                echo $PACTS_cfg->getIndvNavBar($IndividualId, $FamilyId, true, $PACTS_cfg->getUserCanModify(), $PACTS_cfg->getUserCanDelete(), "index.php?view=Home&amp;", "", true, $fields['FirstName'].", ".$fields['Surname']." <i>(".$fields['RoleTypeCode'].")</i>");
            }
            else {
                echo "Error occured while adding Individual";
                $FamilyId = null;
            }
        }
    } else
    {
        if($submitData == "Add New Individual")
        {
            echo "<b>You must enter a FirstName, Surname and select a Family!<p><b>";
        }
    }


    if(is_null($IndividualId))
    {
        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        echo '<div id="details-section">'."\n";
        echo '<form method="post" enctype="multipart/form-data" action="index.php?view=Indv_add">'."\n";
        echo '<div id="viewDetailsC2">';

        if(@$PACTS_cfg->aFieldLookUpTableList["FamilyId"]) {
            echo '<ul><li class="viewDetailsHeader"  title="'.trim($PACTS_cfg->getFieldDescription("Individual", "FamilyId")).'" >Family*</li>';
/*            echo "<li><font class=\"smalltxt\">";

            echo "(".$PACTS_cfg->aFieldLookUpTableList['FamilyId']['Description']."";
            if(@strlen($PACTS_cfg->aFieldLookUpTableList['FamilyId']['Description2'])>0) {
                echo ", ".@$PACTS_cfg->aFieldLookUpTableList['FamilyId']['Description2']."";
            }
            echo ")</font><br />";
*/
            echo '<li class="viewDetailsDataEdit">'."\n";

            if(!is_null($AddToFamilyId)) {
                echo "".$PACTS_cfg->getLookupTableHTML("FamilyId","fields[FamilyId]",$AddToFamilyId, 1);
            } else {
                echo "".$PACTS_cfg->getLookupTableHTML("FamilyId","fields[FamilyId]",@$fields['FamilyId'], 1);
            }

            //			echo "".getLookupTableHTML("FamilyId","fields[FamilyId]",@$fields['FamilyId']);
            echo "\n".'<input type="hidden" name="fields_prev[FamilyId]" value="" />';
            echo '</li></ul>'."\n";
        }
        echo '<ul>'."\n";
        echo '<li class="viewDetailsHeader" title="'.trim($PACTS_cfg->getFieldDescription("Individual", "FirstName")).'" >FirstName*</li>'."\n";

        echo '<li class="viewDetailsDataEdit">'."\n";


        echo '<input type="text" name="fields[FirstName]" size="" value="'.@$fields['FirstName'].'" />'."\n";
        echo '</li>'."\n";
        echo '</ul>'."\n";
        echo '<ul>'."\n";
        echo '<li class="viewDetailsHeader"  title="'.trim($PACTS_cfg->getFieldDescription("Individual", "Surname")),'" >Surname*</li>'."\n";

        echo '<li class="viewDetailsDataEdit">'."\n";
        echo '<input type="text" name="fields[Surname]" value="'.@$fields['Surname'].'" size="" />'."\n";
        echo '</li>'."\n";
        echo '</ul>'."\n";

        if(@$PACTS_cfg->aFieldLookUpTableList["GenderCode"]) {
            echo '<ul><li class="viewDetailsHeader"  title="'.trim($PACTS_cfg->getFieldDescription("Individual", "GenderCode")).'" >GenderCode</li>';

            echo '<li class="viewDetailsDataEdit">'."\n";
/*
            echo "(".$PACTS_cfg->aFieldLookUpTableList['GenderCode']['Description']."";
            if(@strlen($PACTS_cfg->aFieldLookUpTableList['GenderCode']['Description2'])>0) {
                echo ", ".@$PACTS_cfg->aFieldLookUpTableList['GenderCode']['Description2']."";
            }
            echo ")</font><br />";
*/
            echo "".$PACTS_cfg->getLookupTableHTML("GenderCode","fields[GenderCode]",@$fields['GenderCode'],4);
            echo "\n";
            echo '<input type="hidden" name="fields_prev[GenderCode]" value="" />';
            echo "</li></ul>\n";
        }

        if(@$PACTS_cfg->aFieldLookUpTableList["RoleTypeCode"]) {
            echo '<ul><li class="viewDetailsHeader"  title="'.trim($PACTS_cfg->getFieldDescription("Individual", "RoleTypeCode")).'" >RoleTypeCode</li>';
/*
 *
            echo "(".$PACTS_cfg->aFieldLookUpTableList['RoleTypeCode']['Description']."";
            if(@strlen($PACTS_cfg->aFieldLookUpTableList['RoleTypeCode']['Description2'])>0) {
                echo ", ".@$PACTS_cfg->aFieldLookUpTableList['RoleTypeCode']['Description2']."";
            }
            echo ")</font><br />";
*/


            echo '<li class="viewDetailsDataEdit">'."\n";

            echo "".$PACTS_cfg->getLookupTableHTML("RoleTypeCode","fields[RoleTypeCode]",@$fields['RoleTypeCode'],5);
            echo "\n";
            echo '<input type="hidden" name="fields_prev[RoleTypeCode]" value="" />';
            echo "</li></ul>\n";
        }

        echo '<ul><li class="viewDetailsHeader"></li>'."\n";
        echo '<li>'."\n";
        echo '<input type="submit" class="smalltxtInput" name="submitData" value="Add New Individual" />';
        echo '</li>'."\n";
        echo '</ul>'."\n";

        echo '</div>'."\n";
        echo '</form>'."\n";
        echo '<p class="smalltxt">* Mandatory Fields</p><br />'."\n";
echo '</div>'."\n";
    }


}
else {
    echo '<div id="details-section">'."\n";
    echo '<div id="viewDetailsC2">'."\n";

    echo '<p class="errmsg1">Error! You do not have permission to add data!</p>'."\n";
    echo '</div>'."\n";
    echo '</div>'."\n";

} 



?>